#include "PauseState.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "Game.h"
#include "GameplayState.h"
#include "MenuManager.h"
#include "MainMenuState.h"
#include "BitmapFont.h"
#include <sstream>
using namespace std;
#include <fstream>


CPauseState* CPauseState::GetInstance()
{
	static CPauseState s_Instance;
	return &s_Instance;
}
CPauseState::CPauseState(void)
{
	m_pD3D		= nullptr;
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pXA		= nullptr;
	choice = 0;
}


CPauseState::~CPauseState(void)
{
	delete m_pBF;
}

void CPauseState::Enter(void)
{
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();

	m_pBF = new CBitmapFont;
	m_pBF->Initialize();


	LoadVolumes();
}
void CPauseState::Exit()
{
	m_pD3D	= nullptr;
	m_pDI	= nullptr;
	m_pTM	= nullptr;
	m_pXA	= nullptr;
	delete m_pBF;
}
bool CPauseState::Input()
{

	if( m_pDI->KeyPressed( DIK_DOWN ) )
	{
		choice++;
		if( choice == 3 )
			choice = 0;
		if( choice == 0 )
		{
			//CSGD_XAudio2::GetInstance()->MusicPlaySong(m_nBackgroundMusID,true);
			//CSGD_XAudio2::GetInstance()->SFXStopSound(m_nSFXID);
		}
		else if( choice == 1 )
		{
			//CSGD_XAudio2::GetInstance()->MusicStopSong(m_nBackgroundMusID);
			//CSGD_XAudio2::GetInstance()->SFXPlaySound(m_nSFXID);
		}
	}

	if( m_pDI->KeyPressed( DIK_LEFT ) && choice == 0 )
	{
		volume = volume - 0.05f;
		if( volume < 0 )
			volume = 0;
		CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(volume);
	}


	if( m_pDI->KeyPressed( DIK_RIGHT ) && choice == 0 )
	{
		volume = volume + 0.05f;
		if( volume > 1 )
			volume = 1;
		CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(volume);
	}

	if( m_pDI->KeyPressed( DIK_LEFT ) && choice == 1 )
	{
		SFX = SFX - 0.05f;
		if( SFX < 0 )
			SFX = 0;
		CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(SFX);
			//CSGD_XAudio2::GetInstance()->SFXPlaySound(m_nSFXID);

	}


	if( m_pDI->KeyPressed( DIK_RIGHT ) && choice == 1 )
	{
		SFX = SFX + 0.05f;
		if( SFX > 1 )
			SFX = 1;
		CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(SFX);
			//CSGD_XAudio2::GetInstance()->SFXPlaySound(m_nSFXID);

	}
	if( m_pDI->KeyPressed( DIK_UP ) )
	{
		choice--;
		if( choice == -1 )
			choice = 2;
		if( choice == 0 )
		{
			//CSGD_XAudio2::GetInstance()->MusicPlaySong(m_nBackgroundMusID,true);
			//CSGD_XAudio2::GetInstance()->SFXStopSound(m_nSFXID);

		}
	}
	if( m_pDI->KeyPressed( DIK_ESCAPE ) )
		{
			CMenuManager::GetInstance()->GoBack();
			//CGame::GetInstance()->ChangeState(CMenuManager::GetInstance()->PlayBack());
		}

		if( m_pDI->KeyPressed( DIK_RETURN ) && choice == 2)
		{
			CMenuManager::GetInstance()->GoBack();
			//CMenuManager::GetInstance()->PlayBack()->Exit();
			CMenuManager::GetInstance()->GoBack();
			CGame::GetInstance()->ChangeState(CMenuManager::GetInstance()->PlayBack());
		}
	return true;

}
void CPauseState::Update(float fElapsedTime)
{

}
void CPauseState::Render()
{

	string title;
	title = "PAUSE";
	m_pBF->Print(title.c_str(), 70, 40, 1.5);

	ostringstream word;
	word << "VOLUME " << (int)(volume*100);
	m_pBF->Print(word.str().c_str(), 100, 160, 0.5);

	ostringstream word2;

	word2 << "SFX " << (int)(SFX*100);
	m_pBF->Print(word2.str().c_str(), 100, 200, 0.5);

	string exit;
	exit = "EXIT";
	m_pBF->Print(exit.c_str(), 100, 240, 0.5);

	CSGD_TextureManager::GetInstance()->Draw( CMenuManager::GetInstance()->GetImageID(), 70, 160+(choice*40));

}

void CPauseState::SaveVolumes()
{
	ofstream out("music.txt");
	if (out.is_open())
	{
		out << volume;
	}

	ofstream fout("sfx.txt");
	if (fout.is_open())
	{
		fout << SFX;
	}
	fout.close();
}

void CPauseState::LoadVolumes()
{
	ifstream fin;
	fin.open("music.txt");
	if( fin.is_open() )
	{
		fin >> volume;
	}
	fin.close();
	fin.open("sfx.txt");
	if( fin.is_open() )
	{
		fin >> SFX;
	}
}